﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using System.Data.Entity;
using Elderlyhealth.DAL;


public partial class Doctors : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindDoctorsGrid();
        }
    }

    private void BindDoctorsGrid()
    {
        // 使用数据库上下文读取医生数据
        using (var context = new DataClasses1DataContext())
        {
            DoctorsGridView.DataSource = context.Doctors.ToList(); // 获取所有医生
            DoctorsGridView.DataBind(); // 绑定到 GridView
        }
    }

   /* protected void AddDoctorButton_Click(object sender, EventArgs e)
    {
        string name = NameTextBox.Text;
        string specialty = SpecialtyTextBox.Text;
        string licenseNumber = LicenseNumberTextBox.Text;
        string phoneNumber = PhoneNumberTextBox.Text;

        // 使用数据库上下文添加新的医生
        using (var context = new DataClasses1DataContext())
        {
            var doctor = new Doctor
            {
                Name = name,
                Specialty = specialty,
                LicenseNumber = licenseNumber,
                PhoneNumber = phoneNumber,
                CreatedAt = DateTime.Now // 创建时间
            };
            context.Doctors.Add(doctor); // 添加到 Doctors 表
            context.SaveChanges(); // 保存更改
        }

        // 清空输入框
        NameTextBox.Text = "";
        SpecialtyTextBox.Text = "";
        LicenseNumberTextBox.Text = "";
        PhoneNumberTextBox.Text = "";
        BindDoctorsGrid(); // 重新绑定医生列表
    }*/

    protected void DoctorsGridView_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Edit")
        {
            // 编辑功能的实现
        }
    }

    // 模型类定义
    // 在其他地方定义的 DbContext
    public class DataClasses1DataContext : DbContext
    {
        public DbSet<Doctor> Doctors { get; set; } // 确保这里有 Doctors 的 DbSet 定义
    }

    // Doctor 定义
    public class Doctor
    {
        public int DoctorID { get; set; } // 数据库中应设置为自动递增
        public string Name { get; set; }
        public string Specialty { get; set; }
        public string LicenseNumber { get; set; }
        public string PhoneNumber { get; set; }
        public DateTime CreatedAt { get; set; }
    }

    // 在 Doctors.aspx.cs 中的 Button1_Click 处理程序
    protected void Button1_Click(object sender, EventArgs e)
    {
        string name = TextBox1.Text;
        string specialty = TextBox2.Text;
        string licenseNumber = TextBox3.Text;
        string phoneNumber = TextBox4.Text;

        // 使用数据库上下文添加新医生
        using (var context = new DataClasses1DataContext())
        {
            var doctor = new Doctor
            {
                Name = name,
                Specialty = specialty,
                LicenseNumber = licenseNumber,
                PhoneNumber = phoneNumber,
                CreatedAt = DateTime.Now // 创建时间
            };

            context.Doctors.Add(doctor); // 添加到 Doctors 表
            context.SaveChanges(); // 保存更改
        }

        // 清空输入框
        TextBox1.Text = "";
        TextBox2.Text = "";
        TextBox3.Text = "";
        TextBox4.Text = "";

        // 重新绑定医生列表
        BindDoctorsGrid();
    }

}
